<template>
    <div style="display: inline;">
        <editbox ref="input" v-bind:value="value" style="display:inline-block; width: 180px; background: url(Texture/Aries/Creator/keepwork/ggs/user/shuruzhuangdi_16X16_32bits.png#0 0 16 16: 5 5 5 5);"></editbox>
        <button v-on:click="confirm" style="display:inline-block; height: 28px; width: 55px; text-align: center; background: url(Texture/Aries/Creator/keepwork/ggs/user/btn_confirm_16X16_32bits.png#0 0 16 16: 5 5 5 5);">确认</button>
        <button v-on:click="cancel" style="display:inline-block; height: 28px; width: 55px; text-align: center; background: url(Texture/Aries/Creator/keepwork/ggs/user/btn_cancel_16X16_32bits.png#0 0 16 16: 5 5 5 5);">取消</button>
    </div>
</template>

<script type="text/lua">
local KeepWorkItemManager = NPL.load("(gl)script/apps/Aries/Creator/HttpAPI/KeepWorkItemManager.lua");
local IsExistNicknameGSItem = SessionStorage:GetItem("IsExistNicknameGSItem");
if (IsExistNicknameGSItem == nil) then IsExistNicknameGSItem = KeepWorkItemManager.HasGSItem(129) end
echo({IsExistNicknameGSItem or "false"})

function OnRefresh()
    self.value = self.nickname or "";
    GetRef("input"):GetControl():setFocus("click");
end

local function FinishCallback(value, notify)
    if (type(finish) == "function") then
        finish(value);
    end

    if (notify) then
        GameLogic.GetFilters():apply_filters("ggs", {action = "UpdateNickName", nickname = value});
        GameLogic.GetFilters():apply_filters("ggs", {action = "UpdateUserInfo", userinfo = {nickname = value}});
    end
end

function cancel()
    FinishCallback(self.nickname, false);
end

function ShowResultBox(text)
    ui.ShowWindow({
        url = "%ui%/Component/User/ChangeNicknameResult.html",
        width = 500,
        height = 242,
        G = {text = text},
    })
end

function UpdateNickName(value)
    keepwork.user.setinfo({
        router_params = {id = userId},
        nickname = value,
    }, function(status, msg, data) 
        if (status < 200 or status >= 300) then 
            echo(msg);
            ShowResultBox(data and data.message or "修改昵称失败");
            return cancel() 
        end
        if (IsExistNicknameGSItem) then
            ShowResultBox("昵称修改成功, 知识豆扣除 10 个");
            if (type(UserDetail) == "table") then UserDetail.bean = (UserDetail.bean or 0) - 10 end
        else
            ShowResultBox("昵称修改成功");
            IsExistNicknameGSItem = true;
            SessionStorage:SetItem("IsExistNicknameGSItem", IsExistNicknameGSItem);
        end
        FinishCallback(value, true);
    end);
end

function confirm()
    local inputEl = GetRef("input");
    local value = inputEl:GetUIValue();
    if (value == self.nickname or value == "") then return cancel() end
    
    -- 客户端处理铭感词
    value = MyCompany.Aries.Chat.BadWordFilter.FilterString(value);
    
    if (not IsExistNicknameGSItem) then
        return UpdateNickName(value);
    end

    ui.ShowWindow({
        url = "%ui%/Component/User/ChangeNicknameConfirm.html",
        width = 500,
        height = 242,
        G = {
            Confirm = function()
                UpdateNickName(value);
            end,
            Cancel = function()
                cancel();
            end
        }
    })
end 

</script>
